package com.hardware.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hardware.common.CustomException;
import com.hardware.common.R;
import com.hardware.entity.Shop;
import com.hardware.entity.ShopCategory;
import com.hardware.entity.system.Role;
import com.hardware.entity.system.RoleModule;
import com.hardware.mapper.system.RoleMapper;
import com.hardware.service.system.RoleModuleService;
import com.hardware.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleModuleService roleModuleService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<Role> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(Role::getOrderNo);
        //分页查询
        page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @Override
    public void delete(Long id) {
        LambdaQueryWrapper<RoleModule> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(RoleModule::getRoleId,id);
        int count = roleModuleService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联，如果已经关联，抛出一个业务异常
        if(count > 0){
            //已经关联，抛出一个业务异常
            throw new CustomException("当前角色下关联了权限，不能删除");
        }
        //正常删除分类
        super.removeById(id);
    }


}
